home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / cvs-1_3.lha / cvs-1.3 / contrib / commit_prep.pl < prev    next >
Perl Script  |  1992-04-10  |  4KB  |  169 lines

  1. #!/usr/local/bin/perl -w
  2. #
  3. #
  4. # Perl filter to handle pre-commit checking of files.  This program
  5. # records the last directory where commits will be taking place for
  6. # use by the log_accumulate script.  For new file, it forcing the
  7. # existence of a RCS "Id" keyword in the first ten lines of the file.
  8. # For existing files, it checks version number in the "Id" line to
  9. # prevent losing changes because an old version of a file was copied
  10. # into the direcory.
  11. #
  12. # Possible future enhancements:
  13. #
  14. #
  15. #    Check for cruft left by unresolved conflicts.  Search for
  16. #    "^<<<<<<<$", "^-------$", and "^>>>>>>>$".
  17. #
  18. #    Look for a copyright and automagically update it to the
  19. #    current year.
  20. #
  21. # Contributed by David Hampton <hampton@cisco.com>
  22. #
  23.  
  24. ############################################################
  25. #
  26. # Configurable options
  27. #
  28. ############################################################
  29. #
  30. # Check each file (except dot files) for an RCS "Id" keyword.
  31. #
  32. $check_id = 1;
  33.  
  34. #
  35. # Record the directory for later use by the log_accumulate stript.
  36. #
  37. $record_directory = 1;
  38.  
  39. ############################################################
  40. #
  41. # Constants
  42. #
  43. ############################################################
  44. $LAST_FILE     = "/tmp/#cvs.lastdir";
  45. $ENTRIES       = "CVS/Entries";
  46.  
  47. $NoId = "
  48. %s - Does not contain a line with the keyword \"Id:\".
  49.     Please see the template files for an example.\n";
  50.  
  51. # Protect string from substitution by RCS.
  52. $NoName = "
  53. %s - The ID line should contain only \"\$\I\d\:\ \$\" for a newly created file.\n";
  54.  
  55. $BadName = "
  56. %s - The file name '%s' in the ID line does not match
  57.     the actual filename.\n";
  58.  
  59. $BadVersion = "
  60. %s - How dare you!!  You replaced your copy of the file '%s',
  61.     which was based upon version %s, with an %s version based
  62.     upon %s.  Please move your '%s' out of the way, perform an
  63.     update to get the current version, and them merge your changes
  64.     into that file.\n";
  65.  
  66. ############################################################
  67. #
  68. # Subroutines
  69. #
  70. ############################################################
  71.  
  72. sub write_line {
  73.     local($filename, $line) = @_;
  74.     open(FILE, ">$filename") || die("Cannot open $filename, stopped");
  75.     print(FILE $line, "\n");
  76.     close(FILE);
  77. }
  78.  
  79. sub check_version {
  80.     local($i, $id, $rname, $version);
  81.     local($filename, $cvsversion) = @_;
  82.  
  83.     open(FILE, $filename) || die("Cannot open $filename, stopped");
  84.     for ($i = 1; $i < 10; $i++) {
  85.     $pos = -1;
  86.     last if eof(FILE);
  87.     $line = <FILE>;
  88.     $pos = index($line, "Id: ");
  89.     last if ($pos >= 0);
  90.     }
  91.  
  92.     if ($pos == -1) {
  93.     printf($NoId, $filename);
  94.     return(1);
  95.     }
  96.  
  97.     ($id, $rname, $version) = split(' ', substr($line, $pos));
  98.     if ($cvsversion{$filename} == 0) {
  99.     if ($rname ne "\$") {
  100.         printf($NoName, $filename);
  101.         return(1);
  102.     }
  103.     return(0);
  104.     }
  105.  
  106.     if ($rname ne "$filename,v") {
  107.     printf($BadName, $filename, substr($rname, 0, length($rname)-2));
  108.     return(1);
  109.     }
  110.     if ($cvsversion{$filename} < $version) {
  111.     printf($BadVersion, $filename, $filename, $cvsversion{$filename},
  112.            "newer", $version, $filename);
  113.     return(1);
  114.     }
  115.     if ($cvsversion{$filename} > $version) {
  116.     printf($BadVersion, $filename, $filename, $cvsversion{$filename},
  117.            "older", $version, $filename);
  118.     return(1);
  119.     }
  120.     return(0);
  121. }
  122.  
  123. #############################################################
  124. #
  125. # Main Body
  126. #
  127. ############################################################
  128.  
  129. $id = getpgrp();
  130. #print("ARGV - ", join(":", @ARGV), "\n");
  131. #print("id   - ", id, "\n");
  132.  
  133. #
  134. # Suck in the Entries file
  135. #
  136. open(ENTRIES, $ENTRIES) || die("Cannot open $ENTRIES.\n");
  137. while (<ENTRIES>) {
  138.     local($filename, $version) = split('/', substr($_, 1));
  139.     $cvsversion{$filename} = $version;
  140. }
  141.  
  142. #
  143. # Now check each file name passed in, except for dot files.  Dot files
  144. # are considered to be administrative files by this script.
  145. #
  146. if ($check_id != 0) {
  147.     $failed = 0;
  148.     $directory = $ARGV[0];
  149.     shift @ARGV;
  150.     foreach $arg (@ARGV) {
  151.     next if (index($arg, ".") == 0);
  152.     $failed += &check_version($arg);
  153.     }
  154.     if ($failed) {
  155.     print "\n";
  156.     exit(1);
  157.     }
  158. }
  159.  
  160. #
  161. # Record this directory as the last one checked.  This will be used
  162. # by the log_accumulate script to determine when it is processing
  163. # the final directory of a multi-directory commit.
  164. #
  165. if ($record_directory != 0) {
  166.     &write_line("$LAST_FILE.$id", $directory);
  167. }
  168. exit(0);
  169.